using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public static class VUnityEngineExtension
{
    public static GameObject RemoveClone(this GameObject gameObject)
    {
        gameObject.name = gameObject.name.Replace("(Clone)", "");
        return gameObject;
    }

    public static Transform CoverChild(this Transform t, string name)
    {
        Transform ori = t.Find(name);
        if (ori != null) GameObject.DestroyImmediate(ori.gameObject);

        Transform child = new GameObject(name).transform;
        child.transform.SetParent(t);
        child.transform.localPosition = Vector3.zero;
        return child;
    }

    public static Transform UniqueChild(this Transform t, string name)
    {
        Transform ori = t.Find(name);
        if (ori != null) return ori;

        Transform child = new GameObject(name).transform;
        child.transform.SetParent(t);
        child.transform.localPosition = Vector3.zero;
        return child;
    }

    public static GameObject UniqueChild(this Transform t, GameObject prefab)
    {
        Transform ori = t.Find(prefab.name);
        if (ori != null) return ori.gameObject;
        return GameObject.Instantiate(prefab, t);
    }

    public static T UniqueChild<T>(this Transform t,string name) where T :Component
    {
        Transform ori = t.Find(name);
        if (ori != null && ori.gameObject.TryGetComponent<T>(out var comp)) return comp;
        return t.UniqueChild(name).gameObject.UniqueComponent<T>();
    }

    public static void RemoveChild(this Transform t, string name)
    {
        Transform child = null;
        while (FindChild())
        {
            GameObject.DestroyImmediate(child);
        };

        bool FindChild()
        {
            child = t.Find(name);
            if (child != null) return true;
            else return false;
        }
    }

    public static GameObject Instantiate(this GameObject prefab, string name=null)
    {
        var go = GameObject.Instantiate(prefab);
        if(name != null) go.name = name;
        else go.RemoveClone();
        return go;
    }

    public static GameObject FindChild(this Transform t, Func<Transform, bool> condition)
    {
        if (condition == null) return null;
        int count = t.childCount;

        for (int i = 0; i < count; i++)
        {
            var child = t.GetChild(i);
            if (condition(child)) return child.gameObject;
        }
        return null;
    }

    public static T UniqueComponent<T>(this GameObject gameObject) where T : Component
    {
        if (gameObject.TryGetComponent<T>(out var comp))
        {
            return comp;
        }
        else
        {
            return gameObject.AddComponent<T>();
        }
    }
    public static void RemoveComponent<T>(this GameObject gameObject) where T : Component
    {
        while (gameObject.TryGetComponent<T>(out var comp))
        {
            GameObject.DestroyImmediate(comp);
        };
    }

    public static GameObject GlobalUniqueGameObject(this string gameObjectName)
    {
        var go = GameObject.Find(gameObjectName);
        if (go == null)
            go = new GameObject(gameObjectName);
        return go;
    }

    public static void Dismiss<T>(this IList<T> list, Action<T> destroyAction)
    {
        if (list == null) return;
        for (int i = list.Count - 1; i >= 0; i--)
            destroyAction(list[i]);
        list = null;
    }
}



public static class VLLMExtension
{
    public static string ToLLMString(this IEnumerable lists, string head = null)
    {
        List<string> parts = new List<string>();
        foreach (var item in lists)
        {
            if (item != null)
                parts.Add(item.ToString());
        }

        string tolist = string.Join(", ", parts);
        return $"{head}: {{ {tolist} }}";
    }

    public static string ToLLMString<T>(this List<T> list,Func<T,string> tostring, string head = null)
    {
        List<string> parts = new List<string>();
        foreach (var item in list)
        {
            if (item != null)
                parts.Add(tostring(item));
        }

        string tolist = string.Join(", ", parts);
        return $"{tolist}";
    }

    public static string ToLLMString(this IDictionary dictionary, string head = null)
    {
        List<string> parts = new List<string>();
        foreach (DictionaryEntry entry in dictionary)
        {
            if (entry.Key != null && entry.Value != null)
                parts.Add($"{entry.Key}: {entry.Value}");
        }

        string tolist = string.Join(", ", parts);
        return $"{head}: {{ {tolist} }}";
    }

    public static string ToKeysString(this IDictionary dictionary, string head = null)
    {
        List<string> keys = new List<string>();
        foreach (DictionaryEntry entry in dictionary)
        {
            if (entry.Key != null)
                keys.Add(entry.Key.ToString());
        }

        string keyList = string.Join(", ", keys);
        return $"{head}: [ {keyList} ]";
    }

    private static Dictionary<char, char> punctuationMap = new Dictionary<char, char>
    {
        { '，', ',' },
        { '。', '.' },
        { '！', '!' },
        { '？', '?' },
        { '：', ':' },
        { '；', ';' },
        { '（', '(' },
        { '）', ')' },
        { '【', '[' },
        { '】', ']' },
        { '「', '[' },
        { '」', ']' },
        { '『', '[' },
        { '』', ']' },
        { '、', ',' },
        { '《', '<' },
        { '》', '>' },
        { '“', '"' },
        { '”', '"' },
        { '‘', '\'' },
        { '’', '\'' },
        { '－', '-' },
        { '～', '~' },
        { '　', ' ' }, // 全角空格
    };
    public static string ReplaceChinesePunctuation(this string input)
    {
        if (string.IsNullOrEmpty(input)) return input;
        var sb = new System.Text.StringBuilder(input.Length);
        foreach (var ch in input)
        {
            sb.Append(punctuationMap.TryGetValue(ch, out var replacement) ? replacement : ch);
        }
        return sb.ToString();
    }
}
